//
//  CostDataBase.m
//  WWXELuShang
//
//  Created by 王涛 on 15/12/21.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "CostDataBase.h"
static CostDataBase *costDataBase = nil;
@implementation CostDataBase
+ (CostDataBase *)sharedCost {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        costDataBase = [[CostDataBase alloc] init];
    });
    return costDataBase;
}
//指定数据库路径
- (NSString *)sqlitePaths:(NSString *)sqlitePaths {
    //先设置路径，然后拼接sqlitePaths（数据库的名字）
    NSString *sqlPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:sqlitePaths];
    //返回一个以路径＋数据库名字的字符串给我们；
    return sqlPath;
}
//打开数据库
- (void)openSqliteWithPath:(NSString *)paths {
    //获取数据库路径
    //paths为我们数据库名字
    NSString *sqlitePath = [self sqlitePaths:paths];
    self.dataBase = [FMDatabase databaseWithPath:sqlitePath];
    NSLog(@"%@",sqlitePath);
}
//关闭数据库
- (void)closeSqlite {
    [self.dataBase close];
}
//创建一个数据库表
- (void)createSqliteTableWithName:(NSString *)name {
    //判断_dataBase是否存在(是否打开)
    if (!_dataBase) {
        //如果不存在，我们调用创建(打开)的方法
        [self openSqliteWithPath:[NSString stringWithFormat:@"/%@.db",name]];
    }
    NSString *string = [NSString stringWithFormat:@"CREATE TABLE Cost(date TEXT PRIMARY KEY, name TEXT, money TEXT, imagedata blob)"];
    if ([_dataBase open]) {
        BOOL money = [_dataBase executeUpdate:string];
        if (money) {
            NSLog(@"建表成功");
        }else{
            NSLog(@"建表失败");
        }
    }
}

- (void)insertModel:(CostModel *)costModel {
    if (!costModel) {
        NSLog(@"我们的model内为空");
        return;
    }
    if ([_dataBase open]) {
        NSString *insertModel = [NSString stringWithFormat:@"insert into Cost(date,name,money,imagedata) values(?,?,?,?)"];
        BOOL insertSqlite = [_dataBase executeUpdate:insertModel,costModel.date,costModel.name,costModel.money,costModel.imagedata];
        if (insertSqlite) {
            NSLog(@"插入成功");
        }else {
            NSLog(@"插入失败");
        }
    }
}

- (void)deleteWithDate:(NSString *)date {
    if ([_dataBase open]) {
        NSString *deleteMoel = [NSString stringWithFormat:@"delete from Cost where date = ?"];
        BOOL deleteSqlite = [_dataBase executeUpdate:deleteMoel, date];
        if (deleteSqlite) {
            NSLog(@"删除成功");
        }else {
            NSLog(@"删除失败");
        }
    }
}

- (NSMutableArray *)selectAllModel {
    NSMutableArray *allModel = [NSMutableArray array];
    if ([_dataBase open]) {
        NSString *selectSqlite = [NSString stringWithFormat:@"select *from Cost"];
        FMResultSet *resuleSet = [_dataBase executeQuery:selectSqlite];
        while ([resuleSet next]) {
            CostModel *costModel = [[CostModel alloc] init];
            costModel.date = [resuleSet stringForColumn:@"date"];
            costModel.money = [resuleSet stringForColumn:@"money"];
            costModel.name = [resuleSet stringForColumn:@"name"];
            costModel.imagedata = [resuleSet dataForColumn:@"imagedata"];
            [allModel addObject:costModel];
        }
    }
    return allModel;
}





@end
